package com.example.f8399.testdemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.f8399.testdemo.R;
import com.example.f8399.testdemo.bean.AppBean;
import com.example.f8399.testdemo.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lcfeng on 2018/11/27/027
 * Describe:
 */
public class RxAndroidAdapter extends RecyclerView.Adapter<BindingViewHolder> {
    private Context mContext;
    private List<AppBean> plist = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public RxAndroidAdapter(Context context, List<AppBean> list) {
        mContext = context;
        plist = list;
    }


    @NonNull
    @Override
    public BindingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ViewDataBinding inflate = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_rx_manager, parent, false);
        return new BindingViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final BindingViewHolder holder, int position) {
        ViewDataBinding binding = holder.getBinding();
        TextView root = (TextView) binding.getRoot();

        root.setText(plist.get(position).getName());
        root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(holder.getAdapterPosition());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return plist.size();
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }
}
